#!/usr/bin/env python3

# Copyright (c) 2020-2021 Fpemud <fpemud@sina.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.


import subprocess
from ..._util import WinRegistry
from ..._prototype import CustomAction


class DisableScreenSaver(CustomAction):

    def run(self, action_name, s, ts, work_dir):
        # The screen saver data appears in the CONTROL.INI file…
        # [Screen Saver.3DPipes]
        # JointType=0
        # SurfStyle=0
        # TextureQuality=0
        # Tesselation=196
        # Flex=1
        # MultiPipes=1
        # Texture=
        # TextureFileOffset=0

        # And SYSTEM.INI will have a line calling the actual screensaver at the end of the [boot] paragraph:
        # SCRNSAVE.EXE=C:\WINDOWS\SYSTEM\3DPIPE~1.SCR

        with work_dir.mount_image_file(stage=1) as mp:
            subprocess.check_call(["mcopy", "-i", mp.drive_c_loop_file, "::SYSTEM.INI", work_dir.tmp_file])

    def get_after(self):
        return []

    def get_before(self):
        return []


class UseScreenSaver(CustomAction):

    def __init__(self, screen_saver_name, timeout, password):
        assert False

    def run(self, action_name, s, ts, work_dir):
        with work_dir.mount_image_file() as mp:
            reg = WinRegistry(mp.drive_c_dir)
            assert reg.get_value("HKCU\\Control Panel\\Desktop", "ScreenSaveActive", "REG_SZ") == "1"
            reg.modify("HKCU\\Control Panel\\Desktop", "SCRNSAVE.EXE", "REG_SZ", None)
            if True:
                reg.modify("HKCU\\Control Panel\\Desktop", "ScreenSaveTimeOut", "REG_SZ", "300")
            if True:
                reg.modify("HKCU\\Control Panel\\Desktop", "ScreenSaverIsSecure", "REG_SZ", "1")

    def get_after(self):
        return []

    def get_before(self):
        return []


class InstallAndUseScreenSaver(CustomAction):

    def __init__(self, screen_saver_file, timeout, password):
        assert False

    def run(self, action_name, s, ts, work_dir):
        with work_dir.mount_image_file() as mp:
            reg = WinRegistry(mp.drive_c_dir)
            assert reg.get_value("HKCU\\Control Panel\\Desktop", "ScreenSaveActive", "REG_SZ") == "1"
            reg.modify("HKCU\\Control Panel\\Desktop", "SCRNSAVE.EXE", "REG_SZ", None)
            if True:
                reg.modify("HKCU\\Control Panel\\Desktop", "ScreenSaveTimeOut", "REG_SZ", "300")
            if True:
                reg.modify("HKCU\\Control Panel\\Desktop", "ScreenSaverIsSecure", "REG_SZ", "1")

    def get_after(self):
        return []

    def get_before(self):
        return []
